package org.easy.tool.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author Valley
 * Created by Valley on 2024/1/3 17:03
 */
public class ListUtil {

    /**
     * 功能：合并两个List，并去重
     */
    public static <T> List<T> mergeList(List<T> firstList, List<T> secondList) {
        for (T e : secondList) {
            if (!firstList.contains(e)){
                firstList.add(e);
            }
        }
        return firstList;
    }

    /**
     * 功能：将List批量分成若干份, num是每份的最大个数
     */
    public static <T> List<List<T>> splitList(List<T> list, int num) {
        if (list == null || list.isEmpty() || num < 1) {
            return Collections.emptyList();
        }
        List<List<T>> result = new ArrayList<>();
        int size = list.size();
        int count = (size + num - 1) / num;
        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * num, (Math.min((i + 1) * num, size)));
            result.add(subList);
        }
        return result;
    }

    /**
     * 获取列表中的随机元素
     */
    public static <T> T randomElem(List<T> list){
        if (list.size() == 0) return null;
        int randomNum = RandomUtil.randomInt(0, list.size());
        return list.get(randomNum);
    }

    /**
     * 打乱列表元素
     */
    public static void shuffle(List<LinkedHashMap> dataList) {
        Collections.shuffle(dataList);
    }
}
